package com.mdxl.layer_cj.controller;


import com.mdxl.layer_cj.service.MybatisService;
import com.mdxl.layer_cj.shiro.User;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@RequestMapping(value = "/mybatis")
@Controller
public class MybatisController {

    @Autowired
    private MybatisService mybatisService;

    @PostMapping(value = "/add")
    public void add(HttpServletRequest request, HttpServletResponse response){
        String username = ServletRequestUtils.getStringParameter(request,"username",null);
        String password = ServletRequestUtils.getStringParameter(request,"password",null);
        User user       = new User();
        String id       = UUID.randomUUID().toString();
        user.setId(id);
        user.setUsername(username);
        user.setPassword(password);
        mybatisService.add(user);
    }
    @GetMapping(value = "/get")
    @ResponseBody
    public String getUser(HttpServletRequest request){
        String id = "4d438ec3-b699-4910-aa1d-271ab8f35d52";
        User user = mybatisService.getUser(id);
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        return user.toString();
    }

    @DeleteMapping(value = "/delUser")
    @ResponseBody
    public void delUser(){
        String id = "4d438ec3-b699-4910-aa1d-271ab8f35d52";
        mybatisService.delUser(id);
    }
}
